package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SettingDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.SettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by QIUzh on 2020/11/21 17:30
 */
@Service(interfaceClass = SettingService.class)
@Transactional
public class SettingServiceImpl implements SettingService {
    @Autowired
    private SettingDao settingDao;
    //分页
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<Role> page = settingDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }
//增加
    @Override
    public void add(Role role) {
        settingDao.add(role);
    }

    @Override
    public int queryByname(String name,String keyword) {
        return settingDao.queryByname(name,keyword);
    }

    @Override
    public void deleteById(Integer id) {
        //查询是否存在关联用户
        int userCount = settingDao.findUserByid(id);
        int menuCount = settingDao.findMenuByid(id);
        int permissionCount = settingDao.findPermissionByid(id);
        if(userCount > 0){
            throw new RuntimeException("角色有关联信息，无法删除");
        }else if(menuCount > 0){
            throw new RuntimeException("角色有关联信息，无法删除");
        }else if(permissionCount > 0){
            throw new RuntimeException("角色有关联信息，无法删除");
        }
        settingDao.delete(id);
    }

    @Override
    public void edit(Role role) {
        settingDao.edit(role);
    }

    @Override
    public Role findById(Integer id) {
        return settingDao.findById(id);
    }

    @Override
    public List<Integer> findPermissionById(Integer id) {
        return settingDao.findPermissionById(id);
    }

    @Override
    public List<Permission> findPermissionAll() {
        return settingDao.findPermissionAll();
    }

    @Override
    public List<Integer> findPrIdsByid(Integer id) {
        return settingDao.findPrIdsByid(id);
    }

    @Override
    public List<Menu> findMenuAll() {
        return settingDao.findMenuAll();
    }

    @Override
    public List<Integer> findMnIdsByid(Integer id) {
        return settingDao.findMnIdsByid(id);
    }

    @Override
    public void fenPei(Role role, Integer[] checkIds) {

        //获取角色id
        Integer id = role.getId();
        //删除原有数据
        settingDao.deleteRoleAndPermission(id);
        //.往中间表插入数据
        setRoleAndPermission(id, checkIds);

    }

    private void setRoleAndPermission(Integer id, Integer[] checkIds) {
        if (checkIds != null && checkIds.length > 0) {
            for (Integer checkId : checkIds) {
                settingDao.setRoleAndPermission(id, checkId);
            }
        }
    }

    @Override
    public void fenPei2(Role role, Integer[] checkIds2) {

        //获取角色id
        Integer id = role.getId();
        //删除原有数据
        settingDao.deleteRoleAndMenu(id);
        //往中间表插入数据
        setRoleAndMenu(id, checkIds2);

    }

    private void setRoleAndMenu(Integer id, Integer[] checkIds2) {
        if (checkIds2 != null && checkIds2.length > 0) {
            for (Integer checkId2 : checkIds2) {
                settingDao.setRoleAndMenu(id, checkId2);
            }
        }

    }


}
